package com.mingmen.canting.gongyingshang;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.mingmen.canting.R;
import com.mingmen.canting.adapter.MyLabelAdapter;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.http.MyOkhttp;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.GetMarkModel;
import com.mingmen.canting.model.LinkLabelBean;
import com.mingmen.canting.model.ObjectResult;
import com.mingmen.canting.model.ShopListModel;
import com.mingmen.canting.model.SubMitModel;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;
import com.mingmen.canting.util.ToolList;
import com.mingmen.canting.zhenbanlibrary.ClassifyNewAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class GongYingProductDetailFragment extends Fragment implements GongYingProductAdapter.OnItemClickListener {

    private GongYingProductAdapter mAdapter;


    RecyclerView recycle_liebiao;

    LinearLayoutManager linearLayoutManager;



    List<ShopListModel.DataBean> data = new ArrayList<>();

    String token;
    private IntentFilter intentFilter;
    private MyBroadCast netWorkChangeReceiver;

    private class MyBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //系统服务类
            String pos = intent.getStringExtra("pos");
            Log.d("pospospos", pos);
            if (data.size() > 0) {
                data.clear();
            }
            HttpResponse.getFenLeiShop(token, pos, new StringCallback(getActivity()) {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    Log.d("getCodegetStr", s);
                    Gson gson = new Gson();
                    ShopListModel shopListModel = new ShopListModel();
                    shopListModel = gson.fromJson(s, ShopListModel.class);

                    data.addAll(shopListModel.getData());
                    mAdapter.setDataList(data);

                }
            });
        }
    }

    private void init() {

        intentFilter = new IntentFilter();
        intentFilter.addAction("canting.chushizhang.ZhenBanMySortDetailFragment");
        netWorkChangeReceiver = new MyBroadCast();
        //注册广播
        getActivity().registerReceiver(netWorkChangeReceiver, intentFilter);
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gongying, null);


        recycle_liebiao = view.findViewById(R.id.recycle_liebiao);

        Log.d("dkdkkd", "到这里了么");
        String string = getArguments().getString("right");



        init();
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");

        Log.d("到这里了么", string);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        recycle_liebiao.setLayoutManager(linearLayoutManager);

        mAdapter = new GongYingProductAdapter(getActivity(), data);
        recycle_liebiao.setAdapter(mAdapter);


        HttpResponse.getFenLeiShop(token, "100", new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("getCodegetStr", s);
                Gson gson = new Gson();
                ShopListModel shopListModel = new ShopListModel();
                shopListModel = gson.fromJson(s, ShopListModel.class);

                data.addAll(shopListModel.getData());
                mAdapter.setDataList(data);

            }
        });
        mAdapter.setOnItemClickListener(this);

        return view;
    }

    List<String> applyList = new ArrayList<>();


    @Override
    public void onItemClickListener(View view, int pos, List<ShopListModel.DataBean> myLiveList) {
        switch (view.getId()) {
            case R.id.checkbox:
                for (ShopListModel.DataBean cart : data) {
                    if (cart.isChecked()) {   //是否勾上去了
                        Log.d("勾选上了", "勾选上了");
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("commodityId", cart.getCommodityId());
                            jsonObject.put("commodityAmount", cart.getApplyAmount() + "");
                            jsonObject.put("categoryId", "0");
                            jsonObject.put("mark", cart.getMark());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        applyList.add(jsonObject.toString());
                        ToolList.removeDuplicateWithOrder(applyList);
                        Log.d("applyListapplyListapplyList", applyList.toString());
                    } else {
                        Log.d("取消勾选", "取消勾选");
                        if (applyList.size() > 0) {
                            for (int i = 0; i < applyList.size(); i++) {
                                if (applyList.get(i).contains(cart.getCommodityId())) {
                                    applyList.remove(i);
                                }
                            }
                        }
                        ToolList.removeDuplicateWithOrder(applyList);
                        Log.d("applyListapply", applyList.toString());
                    }
                }
                break;

        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(netWorkChangeReceiver);
    }
}
